//
//  LBWComposeToolBar.swift
//  weibo
//
//  Created by Limonwei on 15/12/14.
//  Copyright © 2015年 heima. All rights reserved.
//
//设置下方工具条

import UIKit

//定义一个枚举记录工具条上的几个按钮
enum LBWComposeToolBarButtonType: Int {
    //图片
    case Picture = 0
    //@
    case Mention = 1
    //话题
    case Tread = 2
    //表情
    case Emotion = 3
    //添加
    case Add = 4
}


//iOS9出来的 只支持iOS9以上的版本
class LBWComposeToolBar: UIStackView {
    //定义一个闭包 记录几种按钮类型的点击 参数是类型type
    var childButtonClickClosure: ((type: LBWComposeToolBarButtonType)->())?
    
    private var emoticonButton: UIButton?
    
    // 提供给外界设置当前是否是表情键盘的属性
    // 以便内部切换 emoticonButton 的图标
    var isEmoticonKeyboard: Bool = false {
        didSet{
            var defaultName = "compose_emoticonbutton_background"
            
            if isEmoticonKeyboard {
                defaultName = "compose_keyboardbutton_background"
            }
            //显示表情图标
            emoticonButton?.setImage(UIImage(named: defaultName), forState: .Normal)
            emoticonButton?.setImage(UIImage(named: "\(defaultName)_highlighted"), forState: .Highlighted)
        }
    }


    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //指定排列方向
        axis = .Horizontal
        
        //指定子控件大小填充控件
        distribution = .FillEqually
        
        //添加子控件视图
        setupUI()
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置视图
    private func setupUI(){
        
        //添加按钮
        addChildButton("compose_toolbar_picture", type: .Picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: .Tread)
        //拿到这个表情按钮
        emoticonButton = addChildButton("compose_emoticonbutton_background", type: .Emotion)
        addChildButton("compose_add_background", type: .Add)
        
    }
    
    //定义一个添加按钮的方法
    private func addChildButton(imgName: String, type: LBWComposeToolBarButtonType) -> UIButton {
        
        //定义一个按钮
        let button = UIButton()
        //取到按钮的tag 因为tag的类型是LBWComposeToolBarButtonType,而它的原始值是Int,所以要取到原始值
        button.tag = type.rawValue
        //添加按钮点击
        button.addTarget(self, action: "childButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        //设置不同状态下的图片
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imgName)_highlighted"), forState: .Highlighted)
        //设置背景 利用背景颜色设置背景图片
        button.backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        
        //添加
        addArrangedSubview(button)
        
        return button
    }
    
    //实现按钮点击监听方法
    @objc private func childButtonClick(button: UIButton){
        
        //执行闭包
        childButtonClickClosure?(type: LBWComposeToolBarButtonType(rawValue: button.tag)!)
    }

}

























